package alarm.ruiqi.com.alarm_pro.options.tabs;


import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daimajia.androidanimations.library.BaseViewAnimator;
import com.daimajia.androidanimations.library.YoYo;
import com.lidroid.xutils.util.LogUtils;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ObjectAnimator;

import java.util.List;

import alarm.ruiqi.com.alarm_pro.R;
import alarm.ruiqi.com.alarm_pro.options.MainActivity;

public class GeneralTabFragment extends Fragment {


    private List<Fragment>  fragments;
    private List<String> titles;
    private View mParentView;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;


    public static GeneralTabFragment newInstance(List<Fragment> tabs,List<String> titles){
        GeneralTabFragment fragment = new GeneralTabFragment();
        fragment.setFragments(tabs);
        fragment.setTitles(titles);
        return  fragment;
    }

    //region 公有setter
    public void setTitles(List<String> titles) {
        this.titles = titles;
    }
    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
    }
    //endregion

    //region 生命周期
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().getSupportFragmentManager();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity)(activity)).getHeadFragment().togglrRightDrawable(false);
    }
    @Override
    public void onDetach() {
        super.onDetach();
        ((MainActivity)(getActivity())).getHeadFragment().togglrRightDrawable(true);
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mParentView =  inflater.inflate(R.layout.fragment_general_tab, container, false);
        mViewPager = (ViewPager) mParentView.findViewById(R.id.general_tabs_viewPager);
        // 设置ViewPager的数据等
        mViewPager.setAdapter(new android.support.v4.app.FragmentStatePagerAdapter(getChildFragmentManager()) {

            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }

        });
        mTabLayout = (TabLayout) mParentView.findViewById(R.id.general_tabs_tab_layout);
        for (String title : titles) {
            mTabLayout.addTab(mTabLayout.newTab().setText(title));
        }
//          mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(pageChangeListener);
        mTabLayout.setOnTabSelectedListener(onTabSelectedListener);
        TextView textView = (TextView)((LinearLayout) (((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(0))).getChildAt(1);
        animateText(textView,
                ObjectAnimator.ofFloat(textView, "scaleX", new float[]{1.0F, 0.8F, 0.9F, 1.1F, 1.2F, 1.3F, 1.3F})
                , ObjectAnimator.ofFloat(textView, "scaleY", new float[]{1.0F, 0.8F, 0.9F, 1.1F, 1.2F, 1.3F, 1.3F})
                , ObjectAnimator.ofFloat(textView, "rotation", new float[]{0.0F, -3.0F, -3.0F, 3.0F, -3.0F, 3.0F, -3.0F, 3.0F, -3.0F, 0.0F})
        );
        return mParentView;
    }
    //endregion

    //region binding viewpager 的接口实现
    //============
    private TabLayout.OnTabSelectedListener  onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            if (tabPageChange!=null) tabPageChange.onPageChange(tab);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            if (tabPageChange!=null) tabPageChange.onUnchange(tab);
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            LogUtils.i(tab.toString());
        }
    };

    private TabLayout.TabLayoutOnPageChangeListener pageChangeListener = new TabLayout.TabLayoutOnPageChangeListener(mTabLayout){
        @Override
        public void onPageSelected(int position) {
            if (tabPageChange!=null) tabPageChange.onPageChange(position);
        }
    };




    private interface TabPageChange{
        void onPageChange(Object o);
        void onUnchange(TabLayout.Tab tab);
    }

    private  TabPageChange tabPageChange = new TabPageChange() {
        @Override
        public void onPageChange(Object o) {
            if (o instanceof TabLayout.Tab ){
                mViewPager.setCurrentItem(((TabLayout.Tab) o).getPosition());
            }else {
                TextView textView = (TextView)((LinearLayout) (((ViewGroup) mTabLayout.getChildAt(0)).getChildAt((Integer) o))).getChildAt(1);
                animateText(textView,
                        ObjectAnimator.ofFloat(textView, "scaleX", new float[]{1.0F, 0.8F, 0.9F, 1.1F, 1.2F, 1.3F, 1.3F})
                        , ObjectAnimator.ofFloat(textView, "scaleY", new float[]{1.0F, 0.8F, 0.9F, 1.1F, 1.2F, 1.3F, 1.3F})
                        , ObjectAnimator.ofFloat(textView, "rotation", new float[]{0.0F, -3.0F, -3.0F, 3.0F, -3.0F, 3.0F, -3.0F, 3.0F, -3.0F, 0.0F})
                );
                mTabLayout.getTabAt((Integer) o).select();

            }
        }

        @Override
        public void onUnchange(TabLayout.Tab tab) {
            TextView textView = (TextView)((LinearLayout) (((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(tab.getPosition()) )).getChildAt(1);
            animateText(textView,
                    ObjectAnimator.ofFloat(textView, "scaleX", new float[]{1.3F, 1.25F, 1.2F, 1.15F, 1.1F, 1.05F, 1.0F})
                    , ObjectAnimator.ofFloat(textView, "scaleY", new float[]{1.3F, 1.25F, 1.2F, 1.15F, 1.1F, 1.05F, 1.0F})
                    , ObjectAnimator.ofFloat(textView, "rotation", new float[]{0.0F, -3.0F, -3.0F, 3.0F, -3.0F, 3.0F, -3.0F, 3.0F, -3.0F, 0.0F})
            );
        }


    };

    //new float[]{1.3F, 1.25F, 1.2F, 1.15F, 1.10F, 1.05F, 1.0F})
    private void animateText(TextView textView, final Animator... ans) {
        YoYo.with(new BaseViewAnimator() {
            @Override
            protected void prepare(View target) {
                this.getAnimatorAgent().playTogether(ans);
            }
        }).duration(500).playOn(textView);
    }
    //endregion


}
